package com.zh.freechat.web;

import com.zh.freechat.assemble.ApplyRecordWrapper;
import com.zh.freechat.assemble.UserContactsWrapper;
import com.zh.freechat.common.resp.ApplyRecordVO;
import com.zh.freechat.common.resp.UserContactsVO;
import com.zh.freechat.domain.auth.LoginUser;
import com.zh.freechat.domain.notice.ApplyFriendMessageFactory;
import com.zh.freechat.domain.notice.SseEmitterServer;
import com.zh.freechat.domain.user.entity.ChatUser;
import com.zh.freechat.domain.user.entity.UserContacts;
import com.zh.freechat.domain.user.service.ChatUserService;
import com.zh.freechat.domain.user.service.UserContactsService;
import com.zh.webcommon.common.log.annotation.OperateLog;
import com.zh.webcommon.common.viewdata.ApiResult;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.zh.mycommon.viewdata.ToWrapper.foreach;


/**
 * 联系人管理API
 *
 * @author ZH
 * @date 9:38 2021/8/3
 */
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/contact")
public class ContactsController {
    private UserContactsService userContactsService;
    private SseEmitterServer sseEmitterServer;
    private ChatUserService userService;

    /**
     * 申请添加好友
     */
    @OperateLog("申请添加好友")
    @PostMapping("/apply/{userId}")
    public ApiResult<Void> applyAddFriend(@PathVariable String userId, @RequestParam(defaultValue = "") String remark) {
        var currentUserId = getCurrentUserInfo().getUid();
        userContactsService.applyAdd(currentUserId, userId, remark);
        log.info("User:{} 添加好友id={}成功 ", currentUserId, userId);

        ChatUser user = userService.findById(currentUserId);
        sseEmitterServer.sendMessage(ApplyFriendMessageFactory.create(user, userId));

        return ApiResult.success();
    }

    /**
     * 清除好友申请
     */
    @OperateLog("清除好友申请")
    @PostMapping("/apply-remove/{id}")
    public ApiResult<Void> clearApply(@PathVariable Long id) {
        userContactsService.clearApplyById(id);
        return ApiResult.success();
    }

    /**
     * 清除全部好友申请
     */
    @OperateLog("清除好友申请")
    @PostMapping("/apply-clear/all")
    public ApiResult<Void> clearAllApply() {
        var currentUserId = getCurrentUserInfo().getUid();
        userContactsService.clearAppliesByUser(currentUserId);
        return ApiResult.success();
    }

    /**
     * 同意加好友
     */
    @OperateLog("同意加好友")
    @PutMapping("/agreed/{id}")
    public ApiResult<Void> agreedFriend(@PathVariable Long id) {
        var currentUserId = getCurrentUserInfo().getUid();
        userContactsService.agreedFriend(id, currentUserId);

        return ApiResult.success();
    }

    /**
     * 拒绝加好友
     */
    @OperateLog("拒绝加好友")
    @PutMapping("/refuse/{id}")
    public ApiResult<Void> refuseFriend(@PathVariable Long id) {
        var currentUserId = getCurrentUserInfo().getUid();
        userContactsService.refuseFriend(id, currentUserId);

        return ApiResult.success();
    }

    /**
     * 删除好友
     */
    @OperateLog("删除好友")
    @DeleteMapping("/friend/{userId}")
    public ApiResult<Void> removeContact(@PathVariable String userId) {
        var currentUserId = getCurrentUserInfo().getUid();
        userContactsService.removeContact(currentUserId, userId);

        log.info("User:{} 删除好友id={}成功 ", currentUserId, userId);
        return ApiResult.success();
    }

    /**
     * 加入黑名单
     */
    @OperateLog("加入黑名单")
    @PostMapping("/black/{userId}")
    public ApiResult<Void> addBlack(@PathVariable String userId) {
        var currentUserId = getCurrentUserInfo().getUid();
        userContactsService.addBlack(currentUserId, userId);

        log.info("User:{} 加入黑名单user id={}成功 ", currentUserId, userId);
        return ApiResult.success();
    }

    /**
     * 移出黑名单
     */
    @OperateLog("移出黑名单")
    @DeleteMapping("/black/{userId}")
    public ApiResult<Void> removeBlack(@PathVariable String userId) {
        var currentUserId = getCurrentUserInfo().getUid();
        userContactsService.removeBlack(currentUserId, userId);

        log.info("User:{} 移出黑名单user id={}成功 ", currentUserId, userId);
        return ApiResult.success();
    }

    /**
     * 获取当前用户的联系人列表
     *
     * @return
     */
    @GetMapping("/list/current")
    public ApiResult<UserContactsVO> contacts() {
        var currentUserId = getCurrentUserInfo().getUid();
        UserContacts contacts = userContactsService.contactsByUserId(currentUserId);
        return ApiResult.success(UserContactsWrapper.convertVO(contacts));
    }

    @GetMapping("/apply/list")
    public ApiResult<List<ApplyRecordVO>> applyRecordList() {
        var currentUserId = getCurrentUserInfo().getUid();
        var applyRecordList = userContactsService.findUnsettledApplyRecordList(currentUserId);

        return ApiResult.success(foreach(applyRecordList, ApplyRecordWrapper::convertVO));
    }

    private LoginUser getCurrentUserInfo() {
        return (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

}
